import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:thinking/entity/_.dart';
import 'package:thinking/config/_.dart';
import 'package:thinking/service/TopicService.dart';
import 'package:thinking/utils/_.dart';
import 'package:thinking/widget/_.dart';

class TopicItemWidget extends StatefulWidget{

  final Tag tagData;
  final int type;
  final bool isShowButton;
  final bool isUpdate;
  TopicItemWidget({required this.tagData,required this.type,required Key key,this.isShowButton:true,this.isUpdate:true}):super(key: key);

  @override
  _TopicItemWidget createState() => _TopicItemWidget(tagData: tagData,type: type,isShowButton: isShowButton,isUpdate:isUpdate);

}

class _TopicItemWidget extends State<TopicItemWidget>{

   Tag tagData;
   int type;
   bool isShowButton;
   bool isUpdate;
  _TopicItemWidget({required this.tagData,required this.type,required this.isShowButton,required this.isUpdate});

  @override
  void didUpdateWidget(covariant TopicItemWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    if(oldWidget.isShowButton!=widget.isShowButton){
      setState(() {
         this.isShowButton = widget.isShowButton;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        width: type == 0?Screen.width(context):Screen.width(context) - Screen.setWidth(90),
        child: Panel(
          top: 10,
          bottom: 10,
          child: Column(
            children: [
              Row(
                children: [
                  Expanded(child: ListTile(
                    leading:  ClipRRect(
                      borderRadius: BorderRadius.circular(30),
                      child: ClipOval(
                        child: Image.network(
                          tagData.tagImg!,
                          height: Screen.setWidth(48),
                          width: Screen.setWidth(48),
                          fit: BoxFit.cover,

                        )

                        ,
                      ),
                    ),
                    title: Text(tagData.tagTitle!),
                    subtitle: RichText(
                      text: TextSpan(
                          style: TextStyle(
                              fontSize: 13,
                              color: ColorConfig.TextColor),
                          children: [
                            TextSpan(
                                text: tagData.tagSubNum.toString() + '订阅'
                            ),
                            TextSpan(
                                text: ' '
                            ),
                            TextSpan(
                                text: tagData.tagNum.toString() + '动态'
                            )
                          ]
                      ),

                    ),
                  ),),
                  Visibility(child: TopicButton(isUpdate:isUpdate,isSub: tagData.isSub!,topic: tagData, callBack: () async {
                    tagData.setIsSub = !tagData.isSub!;
                    await TopicService.changeSubTopic(params: {
                      "user_Id": SPreferences().getInt('user_Id'),
                      "topic_Id": tagData.id,
                      "isSub":  tagData.isSub
                    });
                    setState(() {
                      tagData.setTagSubNum = tagData.isSub!?tagData.tagSubNum!+1:tagData.tagSubNum!-1;
                    });
                    BotToast.showText(text: tagData.isSub!?'已订阅~':'已取消');
                  },),visible: this.isShowButton,)
                ],
              ),
              Divider()
            ],
          ),
        ),
      );
  }

}